package com.uzai.console.service.user.impl;

import com.alibaba.fastjson.JSONObject;
import com.uzai.common.entity.User;
import com.uzai.common.utils.EsIndexName;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.constant.ES.ElasticsearchUtil;
import com.uzai.console.service.user.MarketingRewardsService;
import com.uzai.console.vo.user.MarketingRewardVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author liuqi
 * @date 2021年08月25日 20:18
 */
@Service
public class MarketingRewardsServiceImpl implements MarketingRewardsService {

    @Autowired
    private ElasticsearchUtil elasticsearchUtil;

    /**
     * 修改用户的账户余额
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param user
     * @return null
     */
    public void updateMarketingRewards(User user){

        String uzai_marketing_reward_info = EsIndexName.UZAI_MARKETING_REWARD_INFO;
        String marketingRewardId = StringUtils.join(user.getId(), user.getRegSource());
        Map<String, Object> dataMap = elasticsearchUtil.searchDataById(uzai_marketing_reward_info, marketingRewardId, null);
        MarketingRewardVo marketingRewardVo = JSONObject.parseObject(Tools.getStrEmpty(JSONObject.toJSONString(dataMap)), MarketingRewardVo.class);
        if(marketingRewardVo == null){
            marketingRewardVo = new MarketingRewardVo();
        }
        marketingRewardVo.setId(marketingRewardId);

        //绑定上级，奖励上级
        MarketingRewardVo.ReDetail bindReParent = marketingRewardVo.getBindReParent();
        if(bindReParent == null){
            bindReParent = new MarketingRewardVo.ReDetail();
        }
        bindReParent.setUserId(user.getParentId());
        marketingRewardVo.setBindReParent(bindReParent);

        //绑定代理，奖励代理
        MarketingRewardVo.ReDetail bindReAgent = marketingRewardVo.getBindReAgent();
        if(bindReAgent == null){
            bindReAgent = new MarketingRewardVo.ReDetail();
        }
        bindReAgent.setUserId(user.getAgentId());
        marketingRewardVo.setBindReAgent(bindReAgent);

        //绑定合伙人，奖励合伙人
        MarketingRewardVo.ReDetail bindRePartner = marketingRewardVo.getBindRePartner();
        if(bindRePartner == null){
            bindRePartner = new MarketingRewardVo.ReDetail();
        }
        bindRePartner.setUserId(user.getPartnerId());
        marketingRewardVo.setBindRePartner(bindRePartner);

        //修改营销数据ES
        elasticsearchUtil.addDataForObjectSerializer(marketingRewardVo, uzai_marketing_reward_info, marketingRewardId);
    }

}
